﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class HouseAppointmentService : BaseService<HouseAppointment>, IHouseAppointmentService
    {
        private IQueryable<HouseAppointment> GetJoinAll()
        {
            return GetAll()
                 .Include(ha => ha.AdminUser)
                  .Include(ha => ha.User)
                   .Include(ha => ha.House)
                .Include(ha => ha.House.Community)
                .Include(ha => ha.House.Community.Region)
                .Include(ha => ha.House.Community.Region.City);
        }
        public HouseAppointmentDTO[] GetPagedData(long cityId, string status, out long total, int pageIndex, int pageSize)
        {
            var entities = GetJoinAll().Where(ha =>ha.Status== status&& ha.House.Community.Region.CityId == cityId);
            return base.GetPagedData(entities, out total, pageIndex, pageSize).ToList().Select(u => ToDTO(u)).ToArray();
        }

        private HouseAppointmentDTO ToDTO(HouseAppointment houseApp)
        {
            HouseAppointmentDTO dto = new HouseAppointmentDTO();
            dto.CommunityName = houseApp.House.Community.Name;
            dto.CreateDateTime = houseApp.CreateDateTime;
            dto.FollowAdminUserId = houseApp.FollowAdminUserId;
            if (houseApp.AdminUser != null)
            {
                dto.FollowAdminUserName = houseApp.AdminUser.Name;
            }
            dto.FollowDateTime = houseApp.FollowDateTime;
            dto.HouseId = houseApp.HouseId;
            dto.Id = houseApp.Id;
            dto.Name = houseApp.Name;
            dto.PhoneNum = houseApp.PhoneNum;
            dto.RegionName = houseApp.House.Community.Region.Name;
            dto.Status = houseApp.Status;
            dto.UserId = houseApp.UserId;
            dto.VisitDate = houseApp.VisitDate;
            dto.HouseAddress = houseApp.House.Address;
            return dto;
        }

        public long AddNew(long? userId, string name, string phoneNum, long houseId, DateTime visitDate)
        {
            HouseAppointment houseApp = new HouseAppointment();
            houseApp.HouseId = houseId;
            houseApp.Name = name;
            houseApp.PhoneNum = phoneNum;
            houseApp.Status = "未处理";
            houseApp.UserId = userId;
            houseApp.VisitDate = visitDate;
            Add(houseApp);
            return houseApp.Id;
        }

        public bool Follow(long adminUserId, long houseAppointmentId)
        {
            var app = GetById(houseAppointmentId);
            if (app == null)
            {
                throw new ArgumentException("不存在的订单id");
            }
            //FollowAdminUserId不为null，说明要么是自己已经抢过，要么是已经早早的
            //被别人抢了
            if (app.FollowAdminUserId != null)
            {
                return app.FollowAdminUserId == adminUserId;
            }
            //如果/FollowAdminUserId为null，说明有抢的机会
            app.FollowAdminUserId = adminUserId;
            try
            {
                Update(app);
                return true;
            }//如果抛出DbUpdateConcurrencyException说明抢单失败（乐观锁）
            catch (DbUpdateConcurrencyException)
            {
                return false;
            }

        }
    }
}
